/**
 * Created by renzhiqiang on 17/4/23.
 */
import * as usersService from './../../services/home/home';
import pathToRegexp from "path-to-regexp";

export default {
  namespace: 'home',
  state: {
    list: [],
    total: null,
    page: 1,
    isShow: false
    // isShow: true
  },
  reducers: {
    save(state, { payload: { data: list, total, page} }) {
      var list= list.data;
      return { ...state, list, total, page};
    },
    show(state, { payload : { isShow } }) {
      return {...state, isShow}
    }
  },
  effects: {
    *fetch({ payload: { page } }, { call, put, select}) {
      var dataItem =yield select(state => {
        return state.home
      });
      debugger;
      const { data} = yield call(usersService.fetch, { dataItem });
      yield put({
        type: 'save',
        payload: {
          data,
          total: 12,
          page: 10
        },
      });
    },
    *isShow({ preload: { id } }, { call, put, select}) {
      console.info(id);
      var isShow = yield select(state => {
        return !state.home.isShow;
      });
      yield put({
        type: 'show',
        payload: {
          isShow: isShow,
        },
      });
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        const match = pathToRegexp('/home').exec(pathname);
        if (match){
          dispatch({ type: 'fetch', payload: query });
        }
      });
    },
  },
};
